﻿#ifndef _OI_PLSCANNERCORE_WORKER_HPP_
#define _OI_PLSCANNERCORE_WORKER_HPP_

#include "PLScannerCore/OiPLScannerCore.hpp"
#include "Config/OiProductParams.hpp"

namespace Oi 
{ 
    namespace PLScannerCore 
    {
        namespace Environment
        {
            extern bool _startTask;
            extern bool _newParams;
        }

        namespace WorkResult
        {
            enum ResultCode
            {
                RC_Success = 1,
                RC_ProfileEmpty = 100,

                RC_Warning = 200,
                RC_Error = 300,
                RC_Formualr_Disable = 301,
            };

            class OI_PLSCANNER_CORE_API Result
            {
            public:
                int result;
                double kRatio;
                double tick;
                OiResultData data;
                OiProfile profile;
                ProductParams params;
                std::map<double, double> tickProfiles;
                
            public:
                Result();

                Result(const Result& p);

                Result& operator=(const Result& p);

            public:
                static void caculateResult(Result & result, bool dir, void* data);

                static void saveResultToDataBase(const Result & result);

                static void saveResultToFile(const Result & result, const QString path);

                static void loadResultFromDataBase(Result & result, const int id);

                static void loadResultFromFile(Result & result, const QString path);
            };
        }

        class OI_PLSCANNER_CORE_API WorkLoopModeTask : public QThread
        {
            Q_OBJECT

        public:
            WorkLoopModeTask(void* impl, QObject* parent = nullptr);
            void run();

        signals:
            void enterRound();
            void leaveRound();

        private:
            OI_DECLARE_IMPL(WorkLoopModeTask);
            OI_DISABLE_COPY(WorkLoopModeTask);
        };

        class OI_PLSCANNER_CORE_API WorkFixedModeTask : public QThread
        {
            Q_OBJECT

        public:
            WorkFixedModeTask(void* impl, QObject* parent = nullptr);
            void run();

        signals:
            void enterRound();
            void leaveRound();

        private:
            OI_DECLARE_IMPL(WorkFixedModeTask);
            OI_DISABLE_COPY(WorkFixedModeTask);
        };

        class OI_PLSCANNER_CORE_API PLScannerWorker : public QObject
        {
            Q_OBJECT

        public:
            PLScannerWorker(QWidget* parent = 0);
            ~PLScannerWorker();

        public:
            void setParams(const ProductParams& params);
            int isRunning() const;

        public:
            WorkResult::Result result();
            double value();

        signals:
            void enterRound();
            void leaveRound();
            void enterTask();
            void leaveTask();

        public slots:
            void start(int mode = 0);
            void stop();

        public slots:
            void onLeaveTask();

        public slots:
            void product();
            void save(const QString& path);
            void open(const QString& file);

        private:
            OI_DECLARE_IMPL(PLScannerWorker);
            OI_DISABLE_COPY(PLScannerWorker);
        };

    } 
}

#endif
